#import library
library(hydroGOF)
library(clusterSim)
library(sqldf)

regress_linear = function(filePath){
	data = read.csv(filePath)
	good_data = subset(data,data$station!=3)
	
	#data$aod=data.Normalization(data$aod,type="n5",normalization="column")
	#data$temp=data.Normalization(data$temp,type="n5",normalization="column")
	#data$avg_temp=data.Normalization(data$avg_temp,type="n5",normalization="column")
	#data$avg_rh=data.Normalization(data$avg_rh,type="n5",normalization="column")
	#data$avg_preci_24=data.Normalization(data$avg_preci_24,type="n5",normalization="column")
	
	cook_data = good_data
	cook_model = lm(pm25~aod+temp+avg_temp+avg_rh+avg_preci_24,cook_data)
		
	nSample = nrow(cook_data)
	nPredict = length(cook_model$coefficients)-1
	
	cutoff = 4/(nSample-nPredict-1)
	fdist_thresold = qf(0.2,nPredict+1,nSample-nPredict-1)
	
	cookValue = cooks.distance(cook_model)
	good_data["cookValue"] = cookValue
	
	data_4np = subset(good_data,good_data$cookValue<=cutoff)
	model_4np = lm(pm25~aod+temp+avg_temp+avg_rh+avg_preci_24,data_4np)
	data_4np["predict"] = model_4np$fitted.values
	
	
	data_fdist = subset(good_data,good_data$cookValue<=fdist_thresold)
	model_fdist = lm(pm25~aod+temp+avg_temp+avg_rh+avg_preci_24,data_fdist)
	data_fdist["predict"] = model_fdist$fitted.values
	
	write.csv(data_4np,"C:/out/mod_linear_4np.csv")
	write.csv(model_4np$coefficients,"C:/out/mod_linear_4np_coff.csv")
	
	write.csv(data_fdist,"C:/out/mod_linear_fdist.csv")
	write.csv(model_fdist$coefficients,"C:/out/mod_linear_fdist_coff.csv")
	
}
filePath = "C:/in/mod_merge.csv"
regress_linear(filePath)
